package com.example.administrator.shihuo.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.shihuo.R;
import com.example.administrator.shihuo.modle.HomePageModle;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;

import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
public class HomePageAdapter extends MyFragmentBaseAdapter<HomePageModle> {


    private final LayoutInflater layoutInflater;
    private List<HomePageModle> data;
    private LayoutInflater inflate;
    private Context context;


    public HomePageAdapter(List<HomePageModle> data, Context context) {
        super(data);
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolde viewHolde;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.homepager_listview_item, parent, false);
            viewHolde = new ViewHolde();
            viewHolde.imageView = (ImageView) convertView.findViewById(R.id.list_item_iamge);
            viewHolde.itemText = (TextView) convertView.findViewById(R.id.list_item_text);
            viewHolde.itemPrice = (TextView) convertView.findViewById(R.id.list_item_price);
            viewHolde.itemMerchant = (TextView) convertView.findViewById(R.id.list_item_merchant);
            viewHolde.itemNumber = (TextView) convertView.findViewById(R.id.list_item_number);
            viewHolde.top = ((TextView) convertView.findViewById(R.id.list_item_top));
            convertView.setTag(viewHolde);
        } else {
            viewHolde = (ViewHolde) convertView.getTag();
        }

        String path = data.get(position).getPath();

        if (position==2){
    viewHolde.top.setVisibility(View.VISIBLE);
}
        viewHolde.itemMerchant.setText(data.get(position).getItemMerchant());
        viewHolde.itemNumber.setText(data.get(position).getItemNumber());
        viewHolde.itemPrice.setText(data.get(position).getItemPrice());
        viewHolde.itemText.setText(data.get(position).getItemText());
        final ViewHolde finalViewHolde = viewHolde;
        OkHttpUtils.get().url(path).tag("iamge").build().execute(new BitmapCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(Bitmap response) {
                if (response != null) {
                    finalViewHolde.imageView.setImageBitmap(response);
                }

            }
        });

        return convertView;

    }
    public class ViewHolde {
        //图片路径
        private ImageView imageView;
        //商品广告
        private TextView itemText;
        //价格
        private TextView itemPrice;
        //商家
        private TextView itemMerchant;
        //关注人数
        private TextView itemNumber;
        private TextView top;
    }
}
